﻿using Common.Contract.Authentication.Record;
using Common.Contract.SystemConfig.Record;
using SalePerformance.Core.Data;
using SalePerformance.Model.Interface;
using SalePerformance.Model.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SalePerformance.Provider
{
    public class HuntingCustomerProvider : GenericProvider, IHuntingCustomerProvider
    {
        public List<TblHuntingCustomer> RetrieveListCustomerHunting(out int totalRecord, UserRecord dao, FilterRecord textSearch = null, string lang = null, int pagesize = 0, int pageindex = 0, DateTime? fromDate = null)
        {
            var data = repository.DbContext as SalePerformanceEntities;
            var query = from x in data.HuntingCustomers
                        select x;

            DateTime ToDateCompare = DateTime.Now.AddMonths(1);
            DateTime FromDateCompare = DateTime.Now;
            if (!string.IsNullOrEmpty(textSearch.Region) && textSearch.Region.Trim() != "null")
            {
                query = query.Where(x => textSearch.Region.ToUpper().Contains(x.Region.ToUpper()));
            }
            if (!string.IsNullOrEmpty(textSearch.Branch) && textSearch.Branch.Trim() != "null")
            {
                query = query.Where(x => textSearch.Branch.ToUpper().Contains(x.BranhCode.ToUpper()));
            }
            if (!string.IsNullOrEmpty(textSearch.Dao))
            {
                query = query.Where(x => x.Dao.Contains(textSearch.Dao));
            }
            if (!string.IsNullOrEmpty(textSearch.DaoName))
            {
                query = query.Where(x => x.DaoName.Contains(textSearch.DaoName));
            }
            if (!string.IsNullOrEmpty(textSearch.Cif))
            {
                query = query.Where(x => x.CompanyName.Contains(textSearch.Cif));
            }
            if (!string.IsNullOrEmpty(textSearch.Description))
            {
                query = query.Where(x => x.CompanyInfo.Contains(textSearch.Description));
            }
            if (!string.IsNullOrEmpty(textSearch.ProductCategory))
            {
                query = query.Where(x => x.ProductCategory.Contains(textSearch.ProductCategory));
            }
            if (!string.IsNullOrEmpty(textSearch.StatusAccPlan))
            {
                query = query.Where(x => x.ContactStatus.Contains(textSearch.StatusAccPlan));
            }
            if (!string.IsNullOrEmpty(textSearch.CusOpenDate))
            {
                List<string> listFilter = textSearch.CusOpenDate.Split('-').ToList();
                FromDateCompare = DateTime.ParseExact(listFilter.FirstOrDefault().Trim(), "dd/MM/yyyy", CultureInfo.InvariantCulture);
                ToDateCompare = DateTime.ParseExact(listFilter.LastOrDefault().Trim(), "dd/MM/yyyy", CultureInfo.InvariantCulture);
                query = query.Where(x => x.Modifiedtime.Value >= FromDateCompare && x.Modifiedtime.Value <= ToDateCompare);
            }
            totalRecord = query.Count();

            query = query.OrderBy(x => new { x.Region, x.BranchNameSme, x.DaoName });
            if (pagesize > 0 && pageindex >= 0)
            {
                query = query.Skip(pageindex * pagesize).Take(pagesize);
            }
            return query.ToList();
        }

        public TblHuntingCustomer RetrieveCustomerHuntingByLeadId(long leadId)
        {
            var data = repository.DbContext as SalePerformanceEntities;
            var query = from x in data.HuntingCustomers
                        where x.Leadid == leadId
                        select x;
            return query.FirstOrDefault();
        }
    }
}
